package com.xiaoshuidi.cloud.module.report.service.bigdata;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoshuidi.cloud.module.report.controller.bigdata.enums.PieCustomerTypeEnum;
import com.xiaoshuidi.cloud.module.report.controller.bigdata.vo.*;
import com.xiaoshuidi.cloud.module.report.dal.mapper.bigdata.OverViewMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
public class OverViewServiceImpl implements OverViewService {

    private final OverViewMapper overViewMapper;

    @Override
    public OverViewHouseDataVO getOverViewHouseData() {
        //房源概况
        HouseStatisticsVO houseStatistics = overViewMapper.getHouseStatistics();
        //门店统计
        Long apartmentCount = overViewMapper.selectCount();
        //分散式小区
        Long disperseAreaCount = overViewMapper.disperseAreaCount();
        OverViewHouseDataVO overViewHouseDataVO = new OverViewHouseDataVO();
        BeanUtil.copyProperties(houseStatistics, overViewHouseDataVO);
        overViewHouseDataVO.setApartmentCount(apartmentCount);
        overViewHouseDataVO.setDisperseAreaCount(disperseAreaCount);
        //营收
        RevenueVO revenue = overViewMapper.getRevenue();
        overViewHouseDataVO.setTotalAmount(revenue.getTotalAmount());
        overViewHouseDataVO.setRentAmount(revenue.getRentAmount());
        overViewHouseDataVO.setOtherAmount(revenue.getTotalAmount().subtract(revenue.getRentAmount()));
        return overViewHouseDataVO;
    }

    @Override
    public Map<String, List<HouseMapStatisticsVO>> houseMapStatistics() {
        List<HouseMapStatisticsVO> houseMapStatisticsVOS = overViewMapper.houseMapStatistics();
        if (CollectionUtil.isNotEmpty(houseMapStatisticsVOS)) {
            return houseMapStatisticsVOS.stream().filter(f -> StringUtils.isNotEmpty(f.getDistrict())).collect(Collectors.groupingBy(HouseMapStatisticsVO::getDistrict));
        }
        return MapUtil.of();
    }

    @Override
    public List<PieChartResultVO> groupCustomerTypeByCount() {
        List<PieChartResultVO> pieChartResultVOS = overViewMapper.groupCustomerTypeByCount();
        PieCustomerTypeEnum[] values = PieCustomerTypeEnum.values();
        List<PieChartResultVO> list = new ArrayList<>();

        for (PieCustomerTypeEnum value : values) {
            PieChartResultVO build = PieChartResultVO.builder().name(value.getValue()).value(BigDecimal.ZERO).build();
            if (CollectionUtil.isNotEmpty(pieChartResultVOS)) {
                pieChartResultVOS.stream().filter(f->f.getName().equals(value.getType())).findFirst().ifPresent(
                        p->build.setValue(p.getValue())
                );

            }
            list.add(build);
        }

        return list;
    }

    @Override
    public List<PieChartResultVO> personalCustomerAgeIntervalCount() {
         CustomerAgeIntervalVO vo = overViewMapper.personalCustomerAgeIntervalCount();
        List<PieChartResultVO> list = new ArrayList<>();
        list.add(PieChartResultVO.builder().name("20岁以下").value(BigDecimal.valueOf(vo.getTwentyBelow())).build());
        list.add(PieChartResultVO.builder().name("20-30岁").value(BigDecimal.valueOf(vo.getTwentyBetweenThirty())).build());
        list.add(PieChartResultVO.builder().name("30-40岁").value(BigDecimal.valueOf(vo.getThirtyBetweenForty())).build());
        list.add(PieChartResultVO.builder().name("40-50岁").value(BigDecimal.valueOf(vo.getFortyBetweenFifty())).build());
        list.add(PieChartResultVO.builder().name("50岁以上").value(BigDecimal.valueOf(vo.getOverFifty())).build());
        return list;
    }

    @Override
    public List<PieChartResultVO> businessStatisticsCount() {
        BusinessStatisticsVO vo = overViewMapper.businessStatisticsCount();
        if (ObjectUtil.isNotEmpty(vo)) {
            vo.setOtherAmount(vo.getTotalAmount().subtract(vo.getRentAmount()).subtract(vo.getHydropowerAmount()).subtract(vo.getPropertyAmount()).subtract(vo.getStopAmount()));
        }
        List<PieChartResultVO> list = new ArrayList<>();
        list.add(PieChartResultVO.builder().name("租金").value(vo.getRentAmount()).build());
        list.add(PieChartResultVO.builder().name("水电").value(vo.getHydropowerAmount()).build());
        list.add(PieChartResultVO.builder().name("物业").value(vo.getPropertyAmount()).build());
        list.add(PieChartResultVO.builder().name("停车费").value(vo.getStopAmount()).build());
        list.add(PieChartResultVO.builder().name("其他").value(vo.getOtherAmount()).build());
        return list;
    }

    @Override
    public List<ApartmentAvgMonthRentVO> apartmentAvgMonthRentStatistics(Page page) {
        LocalDate date = LocalDate.now();
        // 获取当前月的第一天
        LocalDate firstDay = date.with(TemporalAdjusters.firstDayOfMonth());
        // 获取当前月的最后一天
        LocalDate lastDay = date.with(TemporalAdjusters.lastDayOfMonth());
        return overViewMapper.apartmentAvgMonthRentStatistics(page, firstDay, lastDay);
    }

    @Override
    public List<ApartmentRevenueVO> apartmentRevenueByYear(Page page, Integer year) {
        return overViewMapper.apartmentRevenueByYear(page, year);
    }

    @Override
    public List<StoreCheckInVO> storeCheckInAnalysisByYear(Page page, Integer year) {
        return overViewMapper.storeCheckInAnalysisByYear(page, year);
    }

    @Override
    public List<StoreCheckInVO> storeCheckInAvgAnalysisByYear(Page page, Integer year) {
        List<StoreCheckInVO> storeCheckInVOS = overViewMapper.storeCheckInAnalysisByYear(page, year);
        LocalDate now = LocalDate.now();
        //今年取当前月份 去年取12月
        storeCheckInVOS.forEach(f -> {
            f.setCheckInCount(f.getCheckInCount() / (now.getYear() == year ? now.getMonthValue() : 12));
        });
        return storeCheckInVOS;
    }
}
